package com.chdwmp.product.utils;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil extends StringUtils {

    /**
     * @param formatString
     * @return String
     * @Description: 格式化空字符串为'未知'
     * @author JIHW
     * @date 2016-8-3
     */
    public static String formatStringToUnknown(String formatString) {
        return isEmpty(formatString) ? "未知" : formatString;
    }

    public static String formatNullToEmpty(String formatString) {
        return null == formatString ? "" : formatString;
    }

    /**
     * @param params
     * @return String
     * @Description: 将LIST转化为SQL中（"",""）格式数据
     * @author JIHW
     * @date 2017-1-11
     */
    public static String formatListToString(List<String> params) {
        StringBuffer result = new StringBuffer();
        if (null != params && params.size() > 0) {
            if (isEmpty(result.toString()))
                result.append("(");
            for (String param : params) {
                result.append(param).append(",");
            }
        }

        return isNotEmpty(result.toString()) ? result.toString().substring(0, result.toString().length() - 1) + ")" : "";
    }

    public static String formatListToInteger1(List<Integer> params) {
        StringBuffer result = new StringBuffer();
        if (null != params && params.size() > 0) {
            if (isEmpty(result.toString()))
                result.append("(");
            for (Integer param : params) {
                result.append(param).append(",");
            }
        }

        return isNotEmpty(result.toString()) ? result.toString().substring(0, result.toString().length() - 1) + ")" : "";
    }


    public static String formatListToString3(List<String> params) {
        StringBuffer result = new StringBuffer();
        if (null != params && params.size() > 0) {
            if (isEmpty(result.toString()))
                result.append("(");
            for (String param : params) {
                result.append("'").append(param).append("'").append(",");
            }
        }

        return isNotEmpty(result.toString()) ? result.toString().substring(0, result.toString().length() - 1) + ")" : "";
    }

    public static String formatListToString4(List<String> params) {
        StringBuffer result = new StringBuffer();
        if (null != params && params.size() > 0) {
            if (isEmpty(result.toString()))
                result.append("('");
            for (String param : params) {
                result.append(param).append(",");
            }
        }

        return isNotEmpty(result.toString()) ? result.toString().substring(0, result.toString().length() - 1) + "')" : "";
    }


    public static String getIpAddress(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;

    }

    public static String formatListToString6(List<String> params) {
        StringBuffer result = new StringBuffer();
        if (null != params && params.size() > 0) {
            if (isEmpty(result.toString()))
                result.append("");
            for (String param : params) {
                result.append(param).append(",");
            }
        }

        return isNotEmpty(result.toString()) ? result.toString().substring(0, result.toString().length() - 1) + "" : "";
    }




    public static String formatListToString5(List<String> params) {
        StringBuffer result = new StringBuffer();
        if (null != params && params.size() > 0) {
            if(params.size() > 1){
                for (String param : params) {
                    result.append(param).append("!@##@!");
                }
            }else {
                for (String param : params) {
                    result.append(param);
                }
            }
        }

        return isNotEmpty(result.toString()) ? result.toString().substring(0, result.toString().length())  : "";
    }

    public static String formatListToString4(List<Optional<String>> params, List<String> defa) {
        StringBuffer result = new StringBuffer();
        if (null != params && params.size() > 0) {
            for (int i = 0; i < params.size(); i++) {
                final Optional<String> param = params.get(i);
                if (param.isPresent()) {
                    result.append(param.get()).append(",");
                } else {
                    result.append(defa.get(i)).append(",");
                }
            }
        }
        return isNotEmpty(result.toString()) ? result.toString().substring(0, result.toString().length() - 1)  : "";
    }
    public static String formatListToString5(List<Optional<String>> params, List<String> defa) {
        StringBuffer result = new StringBuffer();
        if (null != params && params.size() > 0) {
            for (int i = 0; i < params.size(); i++) {
                final Optional<String> param = params.get(i);
                if (param.isPresent()) {
                    result.append(param.get()).append("!@##@!");
                } else {
                    result.append(defa.get(i)).append("!@##@!");
                }
            }
        }
        return isNotEmpty(result.toString()) ? result.toString().substring(0, result.toString().length() - 1)  : "";
    }


    public static String formatListToString2(List<String> params) {
        StringBuffer result = new StringBuffer();
        if (null != params && params.size() > 0) {
            for (String param : params) {
                result.append(param).append(",");
            }
        }

        return isNotEmpty(result.toString()) ? result.toString().substring(0, result.toString().length() - 1)  : "";
    }
    public static String formatListToString7(List<String> params) {
        StringBuffer result = new StringBuffer();
        if (null != params && params.size() > 0) {
            for (String param : params) {
                result.append(param).append(",").append("\n");
            }
        }

        return isNotEmpty(result.toString()) ? result.toString().substring(0, result.toString().length() - 1)  : "";
    }
    public static String formatListToInteger(List<Integer> params) {
        StringBuffer result = new StringBuffer();
        if (null != params && params.size() > 0) {
            for (Integer param : params) {
                result.append(param).append(",");
            }
        }

        return isNotEmpty(result.toString()) ? result.toString().substring(0, result.toString().length() - 1)  : "";
    }

    /**
     * 将字符串转换成list
     * @param s
     * @return
     */
    public static List<String> stringConveList(String s) {
        List<String> list = new ArrayList<String>();
        if(StringUtils.isNotEmpty(s)){
            String d[] = s.split(",");
            for (int i = 0; i < d.length; i++) {
                list.add(d[i]);
            }
            return list;
        }
        return list;
    }

    public static List<String> stringConveListByProjectOffer(String s) {
        List<String> list = new ArrayList<String>();
        if(StringUtils.isNotEmpty(s)){
            String d[] = s.split("!@##@!");
            for (int i = 0; i < d.length; i++) {
                list.add(d[i]);
            }
        }
        return list;
    }

    public static String dealUnit(String value){

        String result = "";
        if(StringUtils.isNotEmpty(value)){
            switch (value) {
                case "1":
                    result = "¥";
                    break;
                case "2":
                    result = "€";
                    break;
                case "3":
                    result = "$";
                    break;
            }
        }

        return result;

    }

    public static String dealIsSuccessOrder(Integer value){
        //是否成单 2-否 1-是 3-待定
        String result = "";
        if(value != null){
            switch (value) {
                case 1:
                    result = "是";
                    break;
                case 2:
                    result = "否";
                    break;
                case 3:
                    result = "待定";
                    break;
            }
        }

        return result;

    }

    public static String dealIsBid(Integer value){
        //是否安排竞标 0-否 1-是
        String result = "";
        if(value != null){
            switch (value) {
                case 1:
                    result = "是";
                    break;
                case 0:
                    result = "否";
                    break;
            }
        }

        return result;

    }

    public static String dealBidLanguage(Integer value){
        String result = "";
        if(null != value){

            switch (value) {
                case 1:
                    result = "中文";
                    break;
                case 2:
                    result = "英文";
                    break;
            }
        }

        return result;

    }
    public static String dealIsProjectRecommend(String value){
        String result = "";
        if(StringUtils.isNotEmpty(value)){
            switch (value) {
                case "1":
                    result = "是";
                    break;
                case "0":
                    result = "否";
                    break;
            }
        }

        return result;

    }
    public static String dealProjectType(String value){
        String result = "";
        if(StringUtils.isNotEmpty(value)){

            //1.药物2.器械3.试剂4.非干预5.其他
            switch (value) {
                case "1":
                    result = "药物";
                    break;
                case "2":
                    result = "器械";
                    break;
                case "3":
                    result = "试剂";
                    break;
                case "4":
                    result = "非干预";
                    break;
                case "5":
                    result = "其它";
                    break;
            }
        }

        return result;

    }


    public static String dealPossibility(String value){
        String result = "";
        if(StringUtils.isNotEmpty(value)){

            //成单可能性 1-0 2-30 3-50 4-70 5-100
            switch (value) {
                case "1":
                    result = "0%";
                    break;
                case "2":
                    result = "30%";
                    break;
                case "3":
                    result = "50%";
                    break;
                case "4":
                    result = "80%(70%)";
                    break;
                case "5":
                    result = "100%";
                    break;
            }
        }

        return result;

    }

    public static String dealCenterType(String value){
        String result = "";
        if(StringUtils.isNotEmpty(value)){

            switch (value) {
                case "1":
                    result = "多中心";
                    break;
                case "0":
                    result = "单中心";
                    break;
            }
        }

        return result;

    }

    public static String dealCustomerType(String value){
        String result = "";
        if(StringUtils.isNotEmpty(value)){

            switch (value) {
                case "1":
                    result = "Sponsor";
                    break;
                case "2":
                    result = "CRO";
                    break;
                case "3":
                    result = "SMO";
                    break;
                case "4":
                    result = "Site";
                    break;
            }
        }

        return result;

    }

    public static String dealCustomerImportantLevel(String value){
        String result = "";
        if(StringUtils.isNotEmpty(value)){

            switch (value) {
                case "1":
                    result = "关键客户";
                    break;
                case "2":
                    result = "重要客户";
                    break;
                case "3":
                    result = "普通客户";
                    break;
            }
        }

        return result;

    }


    public static String dealProjectOfferName(String offer_service_type,String offer_version,String offer_date){
       List<String> list = new ArrayList<>();
        if(StringUtils.isNotEmpty(offer_service_type)){
            list.add(offer_service_type);
        }
        if(StringUtils.isNotEmpty(offer_version)){
            list.add(offer_version);
        }
        if(StringUtils.isNotEmpty(offer_date)){
            list.add(offer_date);
        }
        String result = StringUtils.join(list,"-");
        return result;
    }


    public static boolean isChinese(char c) {

        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);

        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS

                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS

                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A

                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION

                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION

                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {

            return true;

        }

        return false;

    }

    public static boolean isEnglish(String charaString){

        return charaString.matches("^[a-zA-Z]*");

    }

    public static boolean isChinese(String str){

        String regEx = "[\\u4e00-\\u9fa5]+";

        Pattern p = Pattern.compile(regEx);

        Matcher m = p.matcher(str);

        if(m.find())

            return true;

        else

            return false;

    }

    public static void main(String[] args) {

        System.out.println(isChinese('员'));

        System.out.println(isChinese('s'));

        System.out.println(isEnglish("程序员之家"));

        System.out.println(isEnglish("robert"));

        System.out.println(isChinese("石药控股集团有限公司-YAS"));

    }

    public static List<String> getFiles(String path) {
        List<String> files = new ArrayList<String>();
        File file = new File(path);
        File[] tempList = file.listFiles();

        for (int i = 0; i < tempList.length; i++) {
            if (tempList[i].isFile()) {
//                files.add(tempList[i].toString());
                //文件名，不包含路径
                String fileName = tempList[i].getName();
                files.add(fileName);
            }
            if (tempList[i].isDirectory()) {
                //这里就不递归了，
            }
        }
        return files;
    }
}